package com.bindatax.spi.log.pentaho;

import com.bindatax.core.log.LogOutInterface;
import com.bindatax.core.log.impl.BaseLogOut;
import org.pentaho.di.core.logging.LogChannel;
import org.pentaho.di.core.logging.LogChannelInterface;

import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

public class BinDataxLogerPentaho extends BaseLogOut implements LogOutInterface {

    private final static Map<String,LogChannelInterface> LOG_MAP = new ConcurrentHashMap<>();

    @Override
    public String getType() {
        return TYPE_PENTAHO;
    }

    @Override
    public void init(Properties logConfig) {
        super.init(logConfig);
    }

    private LogChannelInterface getLog(String name){
        LogChannelInterface log = LOG_MAP.get(name);
        if(log == null){
            log = new LogChannel(name);
            LOG_MAP.put(name,log);
        }
        return log;
    }

    @Override
    public void info(String msg) {
        LogChannelInterface log = getLog(getInvokeName(null));
        log.logBasic(msg);
    }

    @Override
    public void info(String name, String msg) {
        LogChannelInterface log = getLog(getInvokeName(name));
        log.logBasic(msg);
    }

    @Override
    public void info(String msg, Throwable t) {
        LogChannelInterface log = getLog(getInvokeName(null));
        log.logBasic(msg,t);
    }

    @Override
    public void info(String name, String msg, Throwable t) {
        LogChannelInterface log = getLog(getInvokeName(name));
        log.logBasic(msg,t);
    }

    @Override
    public void warn(String msg) {
        info(msg);
    }

    @Override
    public void warn(String name, String msg) {
        info(name,msg);
    }

    @Override
    public void warn(String msg, Throwable t) {
        info(msg,t);
    }

    @Override
    public void warn(String name, String msg, Throwable t) {
        info(name,msg,t);
    }

    @Override
    public void debug(String msg) {
        LogChannelInterface log = getLog(getInvokeName(null));
        log.logDebug(msg);
    }

    @Override
    public void debug(String name, String msg) {
        LogChannelInterface log = getLog(getInvokeName(name));
        log.logDebug(msg);
    }

    @Override
    public void debug(String msg, Throwable t) {
        LogChannelInterface log = getLog(getInvokeName(null));
        log.logDebug(msg,t);
    }

    @Override
    public void debug(String name, String msg, Throwable t) {
        LogChannelInterface log = getLog(getInvokeName(name));
        log.logDebug(msg,t);
    }

    @Override
    public void error(String msg) {
        LogChannelInterface log = getLog(getInvokeName(null));
        log.logError(msg);
    }

    @Override
    public void error(String name, String msg) {
        LogChannelInterface log = getLog(getInvokeName(name));
        log.logError(msg);
    }

    @Override
    public void error(String msg, Throwable t) {
        LogChannelInterface log = getLog(getInvokeName(null));
        log.logError(msg,t);
    }

    @Override
    public void error(String name, String msg, Throwable t) {
        LogChannelInterface log = getLog(getInvokeName(name));
        log.logError(msg,t);
    }
}
